<?php

class SessionUser
{
    /**
     *
     * @var RoleEntity
     */
    private $permissions;

    private $userId;

    public function __construct(array $permissions = array(), $userId = null)
    {
        $this->permissions = $permissions;
        $this->userId = $userId;
        return $this;
    }

    public function getPermissions()
    {
        return $this->permissions;
    }

    public function getUserId()
    {
        return $this->userId;
    }

    /**
     *
     * @param string $subject
     * @param integer $accessLevel
     * @param integer $siteId
     * @param integer $subjectId
     * @return boolean
     */
    public function hasPermission($subject, $accessLevel, $subjectId = null, $siteId = null)
    {
        $accessKey = '';

        if ($siteId != null) {
            $accessKey .= $siteId . '/';
        }

        $accessKey .= $subject;

        if ($subjectId != null) {
            $accessKey .= '/' . $subjectId;
        }

        return (boolean) isset($this->permissions[$accessKey])
            && (int) $this->permissions[$accessKey]->accessLevel >= (int) $accessLevel;
    }
}